﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Report_Change_ReduceUnit_Operate:SqlInteractionBase
    {
        private DataEntity.T_Report_Change_ReduceUnit _objMessage = null;

        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_ReduceUnit_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            this._objMessage = (DataEntity.T_Report_Change_ReduceUnit)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_ReduceUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_ReduceUnit_ProjectID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ProjectID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Building", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Building));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_ReduceUnit_FillDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FillDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Address", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Address));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Operater", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Operater));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_ReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_ReduceUnit_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_FormlyerPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FormlyerPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_NowPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_NowPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_AdjustPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_AdjustPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_Comm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Comm));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_Actual", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Actual));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_AdjustComm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_AdjustComm));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_ReasonID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ReasonID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_FileID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FileID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Reason", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Reason));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Plan_Audit", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Audit));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_ReduceUnit::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_ReduceUnit_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Report_Change_ReduceUnit)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_ReduceUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Building", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Building));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_ReduceUnit_FillDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FillDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Address", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Address));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Operater", SqlDbType.NVarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Operater));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_ReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Change_ReduceUnit_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_FormlyerPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FormlyerPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_NowPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_NowPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_AdjustPrice", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_AdjustPrice));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_Comm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Comm));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_Actual", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Actual));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Change_ReduceUnit_AdjustComm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_AdjustComm));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_ReasonID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_ReasonID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Change_ReduceUnit_FileID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_FileID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Change_ReduceUnit_Reason", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Change_ReduceUnit_Reason));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_ReduceUnit::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Delete(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_ReduceUnit_Delete]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_ReduceUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_ReduceUnit::Delete::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Change_ReduceUnit_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_Report_Change_ReduceUnit");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = new DataEntity.T_Report_Change_ReduceUnit();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Change_ReduceUnit_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    _objMessage.Report_Change_ReduceUnit_ID = (Guid)toReturn.Rows[0]["Report_Change_ReduceUnit_ID"];
                    _objMessage.Report_Change_ReduceUnit_ProjectID = (Guid)toReturn.Rows[0]["Report_Change_ReduceUnit_ProjectID"];
                    _objMessage.Report_Change_ReduceUnit_ExchangeTypeID = (Int32)toReturn.Rows[0]["Report_Change_ReduceUnit_ExchangeTypeID"];
                    _objMessage.Report_Change_ReduceUnit_Building = (string)toReturn.Rows[0]["Report_Change_ReduceUnit_Building"];
                    _objMessage.Report_Change_ReduceUnit_FillDate = (DateTime)toReturn.Rows[0]["Report_Change_ReduceUnit_FillDate"];
                    _objMessage.Report_Change_ReduceUnit_Address = (string)toReturn.Rows[0]["Report_Change_ReduceUnit_Address"];
                    _objMessage.Report_Change_ReduceUnit_Operater = (string)toReturn.Rows[0]["Report_Change_ReduceUnit_Operater"];
                    _objMessage.Report_Change_ReduceUnit_ReportNO = (string)toReturn.Rows[0]["Report_Change_ReduceUnit_ReportNO"];
                    _objMessage.Report_Change_ReduceUnit_ExchangeDate = (DateTime)toReturn.Rows[0]["Report_Change_ReduceUnit_ExchangeDate"];
                    _objMessage.Report_Change_ReduceUnit_FormlyerPrice = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_FormlyerPrice"];
                    _objMessage.Report_Change_ReduceUnit_NowPrice = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_NowPrice"];
                    _objMessage.Report_Change_ReduceUnit_AdjustPrice = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_AdjustPrice"];
                    _objMessage.Report_Change_ReduceUnit_Comm = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_Comm"];
                    _objMessage.Report_Change_ReduceUnit_Actual = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_Actual"];
                    _objMessage.Report_Change_ReduceUnit_AdjustComm = (Decimal)toReturn.Rows[0]["Report_Change_ReduceUnit_AdjustComm"];
                    _objMessage.Report_Change_ReduceUnit_ReasonID = (Int32)toReturn.Rows[0]["Report_Change_ReduceUnit_ReasonID"];
                    _objMessage.Report_Change_ReduceUnit_FileID = (Int32)toReturn.Rows[0]["Report_Change_ReduceUnit_FileID"];
                    _objMessage.Report_Change_ReduceUnit_Reason = (string)toReturn.Rows[0]["Report_Change_ReduceUnit_Reason"];
                    _objMessage.Report_Plan_Audit = (Int32)toReturn.Rows[0]["Report_Plan_Audit"];
                }
                return _objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Change_ReduceUnit::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }
    }
}
